package desktop.core.square;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;

import desktop.conf.SquareConf;

import pulpcore.image.Colors;
import pulpcore.image.CoreImage;

public class SquareFactory {
	private static SquareFactory factory;

	private Map<Integer, CoreImage> squareImages = new LinkedHashMap<Integer, CoreImage>();
	private Random random = new Random(161);

	private SquareFactory() {
		this.squareImages.put(new Integer(0), CoreImage.load("particle.png")
				.tint(Colors.BLUE));
		this.squareImages.put(new Integer(1), CoreImage.load("particle.png")
				.tint(Colors.PURPLE));
		this.squareImages.put(new Integer(2), CoreImage.load("particle.png")
				.tint(Colors.RED));
		this.squareImages.put(new Integer(3), CoreImage.load("particle.png")
				.tint(Colors.YELLOW));
		this.squareImages.put(new Integer(4), CoreImage.load("particle.png")
				.tint(Colors.GREEN));
		this.squareImages.put(new Integer(5), CoreImage.load("particle.png")
				.tint(Colors.GRAY));
		this.squareImages.put(new Integer(6), CoreImage.load("particle.png")
				.tint(Colors.CYAN));
	}

	public synchronized static SquareFactory getFactory() {
		if (factory == null) {
			factory = new SquareFactory();
		}
		return factory;
	}

	private int createSquareId() {
		int id = Math.abs(random.nextInt()) % SquareConf.getMaxType();
		return id;
	}

	/**
	 * square的x，y由bar动态调整
	 */
	public Square createSquare(double x, double y, double width, double height) {
		int id = this.createSquareId();
		return new Square(squareImages.get(id), x, y, width, height, id);
	}
}
